package com.then.component.club.ui;

import android.arch.lifecycle.ViewModelProviders;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.view.View;

import com.luojilab.router.facade.annotation.RouteNode;
import com.then.architecture.view.StickyListGroup;
import com.then.component.club.BR;
import com.then.component.club.R;
import com.then.component.club.entity.Club;
import com.then.component.club.ui.base.ClubModuleActivity;

import timber.log.Timber;

/**
 * Created by 42524 on 2018/1/27.
 */

@RouteNode(path = "/clubListPage", desc = "俱乐部列表")
public class ClubListActivity extends ClubModuleActivity<ClubListDelegate> implements StickyListGroup<Club, String> {

    ClubListActivityViewModel viewModel;

    private ObservableList clubList;

    @Override
    public void actionViewModel(View view, Object o, int actionType) {
        int id = view.getId();
        Timber.d("o:" + o);
        if (id == R.id.cl_club_container) {
            Club club = (Club) o;
            viewDelegate.toast("点击了：" + club.name);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        viewModel = ViewModelProviders.of(this, getViewModelFactory()).get(ClubListActivityViewModel.class);
        clubList = new ObservableArrayList<>();
        viewDelegate.initClubList(clubList, BR.listener, this, this);
        viewModel.getClubList(getPreferences().getUserId()).observe(this, resource -> {
                    if (resource.data != null) {
                        clubList.addAll(resource.data);
                    }
                }
        );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected Class<ClubListDelegate> getDelegateClass() {
        return ClubListDelegate.class;
    }

    @Override
    public int getStickyHeaderId(Club club) {
        return club.type == 1 ? 1 : 2;
    }

    @Override
    public int getHeaderLayoutId() {
        return R.layout.layout_club_header;
    }

    @Override
    public int getStickyHeaderBRId() {
        return BR.vo;
    }

    @Override
    public String getHeaderEntity(Club club) {
        return club.type == 1 ? "我的俱乐部" : "加入的俱乐部";
    }
}
